package com.wzdigit.wms.raw.service.mid;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.mes.basic.api.BasicDataItemDetailApi;
import com.wzdigit.mes.basic.api.BasicVendorApi;
import com.wzdigit.mes.basic.dto.DataItemDetailDTO;
import com.wzdigit.wms.basic.client.response.basic.SkuBaseResponse;
import com.wzdigit.wms.basic.domain.asn.AsnDtl;
import com.wzdigit.wms.basic.domain.asn.AsnHeader;
import com.wzdigit.wms.basic.domain.mid.JobSyncErrorData;
import com.wzdigit.wms.basic.domain.transfer.GdnDtl;
import com.wzdigit.wms.basic.domain.transfer.GdnHeader;
import com.wzdigit.wms.basic.domain.transfer.TransferDtl;
import com.wzdigit.wms.basic.domain.transfer.TransferHeader;
import com.wzdigit.wms.basic.enums.*;
import com.wzdigit.wms.basic.mapper.core.basic.SkuMapper;
import com.wzdigit.wms.basic.mapper.core.mid.JobSyncErrorDataMapper;
import com.wzdigit.wms.basic.mapper.core.order.AsnDtlMapper;
import com.wzdigit.wms.basic.mapper.core.order.AsnHeaderMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.GdnDtlMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.GdnHeaderMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.TransferDtlMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.TransferHeaderMapper;
import com.wzdigit.wms.basic.service.code.CodeMappingService;
import com.wzdigit.wms.raw.constant.SyncTypeConstant;
import com.wzdigit.wms.raw.domain.dto.HouseQaStatus;
import com.wzdigit.wms.raw.domain.dto.IfSappullHeaderDto;
import com.wzdigit.wms.raw.domain.mid.IfSappullDtl;
import com.wzdigit.wms.raw.domain.mid.IfSappullHeader;
import com.wzdigit.wms.raw.exception.SyncException;
import com.wzdigit.wms.raw.mapper.mid.IfSappullDtlMapper;
import com.wzdigit.wms.raw.mapper.mid.IfSappullHeaderMapper;
import net.dreamlu.mica.core.utils.BeanUtil;
import net.dreamlu.mica.core.utils.CollectionUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 接口中间表-采购订单 同步 采购订单
 * </p>
 *
 * @author chao shuai
 * @since 2021-06-15
 */
@Service
public class IfSappullHeaderService {

    @Autowired
    private IfSappullHeaderMapper ifSappullHeaderMapper;

    @Autowired
    private IfSappullDtlMapper ifSappullDtlMapper;

    @Autowired
    private GdnHeaderMapper gdnHeaderMapper;

    @Autowired
    private GdnDtlMapper gdnDtlMapper;

    @Autowired
    private AsnHeaderMapper asnHeaderMapper;

    @Autowired
    private AsnDtlMapper asnDtlMapper;

   @Autowired
   private TransferHeaderMapper transferHeaderMapper;

   @Autowired
   private TransferDtlMapper transferDtlMapper;

    @Autowired
    private JobSyncErrorDataMapper jobSyncErrorDataMapper;

    @Reference(version = "${dubbo.consumers.basic.vendor.api:1.0.0}", check = false, timeout = 300000)
    private BasicVendorApi  basicVendorApi;

    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private SyncErrorDataService syncErrorDataService;

    @Autowired
    CodeMappingService codeMappingService;

    @Reference(version = "${dubbo.consumers.basic.data.item.detail.api:1.0.0}", check = false, timeout = 300000)
    private BasicDataItemDetailApi dataItemDetailApi;


    @Value(value = "${default_owner.code}")
    private String DEFAULT_OWNER;

    @Value(value = "${default_owner.name}")
    private String DEFAULT_OWNER_NAME;

    @Autowired
    MappingHouseService mappingHouseService;

    private final String syncType = SyncTypeConstant.IF_PULL_TO_GDN_OR_ASN;

    public List<IfSappullHeaderDto> selectSyncData() {
        ArrayList<IfSappullHeaderDto> ifSappullHeaderDtos = new ArrayList<>();
        QueryWrapper<IfSappullHeader> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("Attribute10","1").or().isNull("Attribute10");
        List<IfSappullHeader> selectSyncData = ifSappullHeaderMapper.selectList(queryWrapper);
        if (!CollectionUtil.isEmpty(selectSyncData)) {
            for (IfSappullHeader ifSappullHeader : selectSyncData) {
                IfSappullHeaderDto ifSappullHeaderDto = new IfSappullHeaderDto();
                BeanUtil.copyNonNull(ifSappullHeader, ifSappullHeaderDto);
                List<IfSappullDtl> ifSalesreturnDtls = ifSappullDtlMapper.selectList(new QueryWrapper<>(
                        new IfSappullDtl().setIfSappullHeaderId(ifSappullHeader.getIfSappullHeaderId())
                ));
                ifSappullHeaderDto.setIfSappullDtlList(ifSalesreturnDtls);
                ifSappullHeaderDtos.add(ifSappullHeaderDto);
            }

        }
        return ifSappullHeaderDtos;
    }




    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void syncDataGdn(IfSappullHeaderDto data, boolean isSyncError) throws Exception {
        String rsnum = data.getRsnum(); //
        GdnHeader gdnHeaderRecord = gdnHeaderMapper.selectOne(new QueryWrapper<>(new GdnHeader().setGdnNo(rsnum)));
        //删除逻辑
        //4类型涉及明细和主表删除 主表删除XLOEK字段写入X 对应明细删除再写入 明细删除XLOEK字段写入X
        if("X".equals(data.getXloek())){
            if(gdnHeaderRecord==null){
                //throw new SyncException(syncType, rsnum,  "没有查询到单号为:" + rsnum+ "的订单 ,无法删除");

            }else {
                gdnDtlMapper.delete(new QueryWrapper<GdnDtl>(new GdnDtl().setGdnHeaderId(gdnHeaderRecord.getGdnHeaderId())));//删除明细
                gdnHeaderMapper.deleteById(gdnHeaderRecord.getGdnHeaderId());//删除主表数据
            }
            int row = ifSappullHeaderMapper.updateFlagById(data.getIfSappullHeaderId());
            return ;
        }

        //查询仓库//
        //处理仓库信息
        //获取仓库
        HouseQaStatus houseQaStatus = mappingHouseService.getWmsHourseIdBySapHourseCode(data.getLgort());//移出库位
        if(houseQaStatus==null){
            throw new SyncException(syncType, rsnum, "SAP仓库编码" + data.getLgort() + "无法转换成WMS仓库");
        }
        String inWhs = data.getUmlgo();


        List<IfSappullDtl> ifSappullDtlList = data.getIfSappullDtlList();

        GdnHeader gdnHeader = new GdnHeader();
        //同步主表信息
        gdnHeader.setGdnNo(rsnum);
        //设置单据类型
        switch (data.getBztp()){
            case "1":
                gdnHeader.setGdnType(GdnTypeEnum.D14.code);
                 break;
            case "2":
                gdnHeader.setGdnType(GdnTypeEnum.D15.code);
                break;
            case "5":
                //移入库位 类型5先出后入 才需要转换
                if("3-1".equals(data.getAttribute1())){ //调拨类型需要格式化两个
                    HouseQaStatus houseQaStatusIn = mappingHouseService.getWmsHourseIdBySapHourseCode(data.getUmlgo());
                    if(houseQaStatusIn==null){
                        throw new SyncException(syncType, rsnum, "SAP仓库编码" + data.getUmlgo() + "无法转换成WMS仓库");
                    }
                    inWhs = houseQaStatusIn.getWhsId()+"";
                    gdnHeader.setGdnType(GdnTypeEnum.D29.code);
                }else {
                    gdnHeader.setGdnType(GdnTypeEnum.D23.code);
                }

                break;
            default:
                throw new SyncException(syncType, rsnum,  "单据类型【" + data.getBztp()+ "】无法转换");
        }


        gdnHeader.setFactory(data.getWerks());
        gdnHeader.setErpMoveType(data.getBwart());
        gdnHeader.setGdnCreateTime(data.getCreateTime());
        gdnHeader.setGdnCreateUser(data.getCreateUser());
        gdnHeader.setShipDate(new Date());


        DataItemDetailDTO dataItem = dataItemDetailApi.getDefaultDataItemDetailByCode(DEFAULT_OWNER);
        gdnHeader.setOwner(dataItem == null || dataItem.getItemValue() == null ? 0: Integer.valueOf(dataItem.getItemValue()));


        if (gdnHeaderRecord == null) {
            gdnHeader.setCreateUser("system");
            gdnHeader.setCreateTime(new Date());
            gdnHeader.setStatus( GdnHeaderStatusEnum.CREATE.getCode());//新建状态
            gdnHeaderMapper.insert(gdnHeader);
        } else {
            gdnHeader.setModifyTime(new Date());
            gdnHeader.setModifyUser("system");
            gdnHeader.setGdnHeaderId(gdnHeaderRecord.getGdnHeaderId());
            gdnHeaderMapper.updateById(gdnHeader);
        }
        //同步明细 内部自己生成行号
        //获取最大行号
        Integer maxLineNo = gdnDtlMapper.selectMaxLineId(gdnHeader.getGdnHeaderId());
        if(maxLineNo == null){
            maxLineNo = 1 ;
        }

        for (IfSappullDtl ifSappullDtl : ifSappullDtlList) {
            GdnDtl gdnDtlRecord = gdnDtlMapper.selectOne(new QueryWrapper<>(new GdnDtl().setGdnHeaderId(gdnHeader.getGdnHeaderId()).setSourceShipNoticeItem(ifSappullDtl.getRspos())));
            if("X".equals(ifSappullDtl.getXloek())){
                if(gdnDtlRecord==null){//查不到明细行 直接抛异常
                    continue;
                    //throw new SyncException(syncType, rsnum, "没有查询到要删除的明细行，行号【"+ifSappullDtl.getRspos()+"】,删除失败");
                }else{
                    gdnDtlMapper.deleteById(gdnDtlRecord.getGdnDtlId());
                }
                continue;
            }

            //查询SKUID
            SkuBaseResponse sku = skuMapper.getSkuByCode(ifSappullDtl.getMatnr());//SKU
            if(sku==null||sku.getSkuId()==null){
                throw new SyncException(syncType, rsnum, "物料编码" + ifSappullDtl.getMatnr() + "不存在");
            }

            GdnDtl gdnDtl = new GdnDtl();
            gdnDtl.setGdnHeaderId(gdnHeader.getGdnHeaderId());
            gdnDtl.setSourceShipNoticeItem(ifSappullDtl.getRspos());//行号
            gdnDtl.setSkuId(sku.getSkuId());
            gdnDtl.setOrderQty(ifSappullDtl.getMenge());
            gdnDtl.setUnit(ifSappullDtl.getMeins());
            gdnDtl.setRequireTime(ifSappullDtl.getBdter());
            gdnDtl.setWhsId(houseQaStatus.getWhsId());
            gdnDtl.setUdf5(inWhs);//同步库位

            if (gdnDtlRecord == null) {
                gdnDtl.setCreateUser("system");
                gdnDtl.setCreateTime(new Date());
                gdnDtl.setQaStatus(QaStatusEnum.OK.code);
                gdnDtl.setStatus( GdnDtlStatusEnum.CREATE.getCode());//新建状态
                gdnDtl.setGdnLineNo(maxLineNo++);
                try {
                    gdnDtlMapper.insert(gdnDtl);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new SyncException(syncType, rsnum, e.getMessage());
                }
            } else {
                gdnDtl.setModifyUser("system");
                gdnDtl.setModifyTime(new Date());
                gdnDtl.setGdnDtlId(gdnDtlRecord.getGdnDtlId());
                gdnDtl.setGdnLineNo(gdnDtlRecord.getGdnLineNo());
                try {
                    gdnDtlMapper.updateById(gdnDtl);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new SyncException(syncType, rsnum, e.getMessage());
                }
            }
            int row = ifSappullHeaderMapper.updateFlagById(data.getIfSappullHeaderId());
            //检查是否有同步失败的记录，有则更新为成功
            JobSyncErrorData jobSyncErrorData = jobSyncErrorDataMapper.getSyncErrorDataByTypeAndCode(syncType, rsnum);
            if (jobSyncErrorData != null && 0 == jobSyncErrorData.getIsSuccess()) {
                syncErrorDataService.updateSuccessByCode(syncType, rsnum);
            }
        }

    }


    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void syncDataTransfer(IfSappullHeaderDto data, boolean isSyncError) throws Exception {
        String rsnum = data.getRsnum(); //委外单号--》发货单号or收货通知单号
        TransferHeader transferHeaderRecord = transferHeaderMapper.selectOne(new QueryWrapper<>(new TransferHeader().setTransferNo(rsnum)));
        if("X".equals(data.getXloek())){
            if(transferHeaderRecord==null){
               // throw new SyncException(syncType, rsnum,  "没有查询到单号为:" + rsnum+ "的调拨单 ,无法删除");
            }else {
                transferDtlMapper.delete(new QueryWrapper<TransferDtl>(new TransferDtl().setTransferHeaderId(transferHeaderRecord.getTransferHeaderId())));//删除明细
                transferHeaderMapper.deleteById(transferHeaderRecord.getTransferHeaderId());//删除主表数据
                //return ; //删除后直接跳过本条数据
            }
            int row = ifSappullHeaderMapper.updateFlagById(data.getIfSappullHeaderId());
            return ;
        }
        //查询仓库信息
        HouseQaStatus houseQaStatusOut = mappingHouseService.getWmsHourseIdBySapHourseCode(data.getLgort());
        if(houseQaStatusOut==null){
            throw new SyncException(syncType, rsnum,  "仓库" + data.getLgort() + "不存在");
        }
        HouseQaStatus houseQaStatusIn = mappingHouseService.getWmsHourseIdBySapHourseCode(data.getUmlgo());
        if(houseQaStatusIn==null){
            throw new SyncException(syncType, rsnum,  "仓库" + data.getUmlgo() + "不存在");
        }


        List<IfSappullDtl> ifSappullDtlList = data.getIfSappullDtlList();

        TransferHeader transferHeader = new TransferHeader();
        //同步主表信息
        transferHeader.setTransferNo(data.getRsnum());
        //设置单据类型
        switch (data.getBztp()){
            case "4":
                transferHeader.setTransferType(TransferTypeEnum.T08.code);
                break;
            case "5":
                transferHeader.setTransferType(TransferTypeEnum.T07.code);
                break;
            default:

                throw new SyncException(syncType, rsnum,  "单据类型【" + data.getBztp()+ "】无法转换");
        }
        transferHeader.setFmWhs(houseQaStatusOut.getWhsId());
        transferHeader.setToWhs(houseQaStatusIn.getWhsId());
        transferHeader.setErpMoveType(data.getBwart());
        transferHeader.setCreateUser(data.getCreateUser());
        transferHeader.setCreateTime(data.getCreateTime());




         if (transferHeaderRecord == null) {
            transferHeader.setStatus(TransferHeaderStatusEnum.CREATE.code);  //到货通知单状态
            transferHeaderMapper.insert(transferHeader);
        } else {
            transferHeader.setModifyTime(new Date());
            transferHeader.setModifyUser("system");
            transferHeader.setTransferHeaderId(transferHeaderRecord.getTransferHeaderId());
            transferHeaderMapper.updateById(transferHeader);
        }
        //同步明细



        for (IfSappullDtl ifSappullDtl : ifSappullDtlList) {
            TransferDtl transferDtlRecord = transferDtlMapper.selectOne(new QueryWrapper<>(new TransferDtl().setTransferHeaderId(transferHeader.getTransferHeaderId()).setTransferLineNo(ifSappullDtl.getRspos())));
            if("X".equals(ifSappullDtl.getXloek())){
                if(transferDtlRecord==null){//查不到明细行 直接抛异常
                    throw new SyncException(syncType, rsnum, "没有查询到要删除的明细行，行号【"+ifSappullDtl.getRspos()+"】,删除失败");
                }else{
                    gdnDtlMapper.deleteById(transferDtlRecord.getTransferDtlId());
                }
                continue;
            }

            SkuBaseResponse sku = skuMapper.getSkuByCode(ifSappullDtl.getMatnr());
            if(sku==null||sku.getSkuId()==null){
                throw new SyncException(syncType, rsnum, "物料编码" + ifSappullDtl.getMatnr() + "不存在");
            }

            TransferDtl transferDtl = new TransferDtl();
            transferDtl.setTransferHeaderId(transferHeader.getTransferHeaderId());
            DataItemDetailDTO dataItem = dataItemDetailApi.getDefaultDataItemDetailByCode(DEFAULT_OWNER);
           // DataItemDetailDTO dataItem = dataItemDetailApi.getDefaultDataItemDetailByCode(DEFAULT_OWNER);

            transferDtl.setFmOwner(dataItem == null || dataItem.getItemValue() == null ? 0: Integer.valueOf(dataItem.getItemValue()));//待定
            transferDtl.setToOwner(dataItem == null || dataItem.getItemValue() == null ? 0: Integer.valueOf(dataItem.getItemValue()));//待定

            transferDtl.setTransferNo(transferHeader.getTransferNo());
            transferDtl.setTransferLineNo(ifSappullDtl.getRspos());
            transferDtl.setFmSku(sku.getSkuId());
            transferDtl.setToSku(sku.getSkuId());
            transferDtl.setFmQaStatus(houseQaStatusOut.getQaStatus());//取仓库品质状态
            transferDtl.setToQaStatus(houseQaStatusIn.getQaStatus());
            String orderQtyStr = ifSappullDtl.getMenge().toString();
            transferDtl.setOrderQty(Integer.parseInt(orderQtyStr));
            transferDtl.setTransitQty(Integer.parseInt(orderQtyStr));
            transferDtl.setCmpQty(0);//待定
            transferDtl.setUnit(ifSappullDtl.getMeins());
            transferDtl.setCreateTime(ifSappullDtl.getCreateTime());
            transferDtl.setCreateUser(ifSappullDtl.getCreateUser());
            transferDtl.setFmFactory(data.getWerks());
            transferDtl.setToFactory(data.getWerks());
            if (transferDtlRecord == null) {
                transferDtl.setStatus(TransferDtlStatusEnum.CREATE.getCode());//新建状态
                try {
                    transferDtlMapper.insert(transferDtl);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new SyncException(syncType, rsnum, e.getMessage());
                }
            } else {
                transferDtl.setModifyUser("system");
                transferDtl.setModifyTime(new Date());
                transferDtl.setTransferDtlId(transferDtlRecord.getTransferDtlId());
                try {
                    transferDtlMapper.updateById(transferDtl);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new SyncException(syncType, rsnum, e.getMessage());
                }
            }
            int row = ifSappullHeaderMapper.updateFlagById(data.getIfSappullHeaderId());
            //检查是否有同步失败的记录，有则更新为成功
            JobSyncErrorData jobSyncErrorData = jobSyncErrorDataMapper.getSyncErrorDataByTypeAndCode(syncType, rsnum);
            if (jobSyncErrorData != null && 0 == jobSyncErrorData.getIsSuccess()) {
                syncErrorDataService.updateSuccessByCode(syncType, rsnum);
            }
        }

    }

    public void syncDataAsn(IfSappullHeaderDto data, boolean isSyncError) throws SyncException {
        String rsnum = data.getRsnum(); //
        AsnHeader asnHeaderRecord = asnHeaderMapper.selectOne(new QueryWrapper<>(new AsnHeader().setAsnNo(rsnum)));
        //删除逻辑
        //4类型涉及明细和主表删除 主表删除XLOEK字段写入X 对应明细删除再写入 明细删除XLOEK字段写入X
        if("X".equals(data.getXloek())){
            if(asnHeaderRecord==null){
                //throw new SyncException(syncType, rsnum,  "没有查询到单号为:" + rsnum+ "的订单 ,无法删除");
            }else {
                asnDtlMapper.delete(new QueryWrapper<AsnDtl>(new AsnDtl().setAsnHeaderId(asnHeaderRecord.getAsnHeaderId())));//删除明细
                asnHeaderMapper.deleteById(asnHeaderRecord.getAsnHeaderId());//删除主表数据
            }
            int row = ifSappullHeaderMapper.updateFlagById(data.getIfSappullHeaderId());
            return ;
        }

        //查询仓库//
        //处理仓库信息
        //处理仓库信息
        HouseQaStatus houseQaStatus = mappingHouseService.getWmsHourseIdBySapHourseCode(data.getUmlgo());
        if(houseQaStatus==null){
            throw new SyncException(syncType, rsnum, "SAP仓库编码" + data.getUmlgo()+ "无法转换成WMS仓库");
        }

        List<IfSappullDtl> ifSappullDtlList = data.getIfSappullDtlList();

        AsnHeader asnHeader = new AsnHeader();
        //同步主表信息
        asnHeader.setAsnNo(rsnum);
        asnHeader.setAsnType(AsnTypeEnum.R17.code);

        asnHeader.setFactory(data.getWerks());
        asnHeader.setErpMoveType(data.getBwart());
        asnHeader.setAsnCreateUser(data.getCreateUser());
        asnHeader.setAsnCreateTime(data.getCreateTime());
        asnHeader.setReleaseStatus(0);



        DataItemDetailDTO dataItem = dataItemDetailApi.getDefaultDataItemDetailByCode(DEFAULT_OWNER);
        asnHeader.setOwner(dataItem == null || dataItem.getItemValue() == null ? 0: Integer.valueOf(dataItem.getItemValue()));


        if (asnHeaderRecord == null) {
            asnHeader.setCreateUser("system");
            asnHeader.setCreateTime(new Date());
            asnHeader.setStatus( AsnHeaderStatusEnum.CREATE.getCode());//新建状态
            asnHeader.setQcStatus(QcStatusEnum.已创建质检单.getCode());
            asnHeaderMapper.insert(asnHeader);
        } else {
            asnHeader.setModifyTime(new Date());
            asnHeader.setModifyUser("system");
            asnHeader.setAsnHeaderId(asnHeaderRecord.getAsnHeaderId());
            asnHeaderMapper.updateById(asnHeader);
        }
        //同步明细 asn直接同步到
        //获取最大行号
       /* Integer maxLineNo = asnDtlMapper.selectMaxLineId(asnHeader.getAsnHeaderId());
        if(maxLineNo == null){
            maxLineNo = 1 ;
        }*/

        for (IfSappullDtl ifSappullDtl : ifSappullDtlList) {
            AsnDtl asnDtlRecord = asnDtlMapper.selectOne(new QueryWrapper<>(new AsnDtl().setAsnHeaderId(asnHeader.getAsnHeaderId()).setAsnRef5(ifSappullDtl.getRspos())));
            if("X".equals(ifSappullDtl.getXloek())){
                if(asnDtlRecord==null){//查不到明细行 直接抛异常
                    continue;
                    //throw new SyncException(syncType, rsnum, "没有查询到要删除的明细行，行号【"+ifSappullDtl.getRspos()+"】,删除失败");
                }else{
                    asnDtlMapper.deleteById(asnDtlRecord.getAsnDtlId());
                }
                continue;
            }

            //查询SKUID
            SkuBaseResponse sku = skuMapper.getSkuByCode(ifSappullDtl.getMatnr());//SKU
            if(sku==null||sku.getSkuId()==null){
                throw new SyncException(syncType, rsnum, "物料编码" + ifSappullDtl.getMatnr() + "不存在");
            }



            AsnDtl asnDtl = new AsnDtl();
            asnDtl.setAsnHeaderId(asnHeader.getAsnHeaderId());
            asnDtl.setAsnRef5(ifSappullDtl.getRspos());
            asnDtl.setSkuId(sku.getSkuId());
            asnDtl.setOrderQty(ifSappullDtl.getMenge());
            asnDtl.setOrderQtyEa(ifSappullDtl.getMenge());
            asnDtl.setUnit(sku.getBaseUnit());
            asnDtl.setAsnLineNo(ifSappullDtl.getRspos());
            asnDtl.setAsnNo(asnHeader.getAsnNo());
            //TODO 不知道这个字段要同步到哪里
            //asnDtl.setship(ifSappullDtl.getBdter());
            asnDtl.setWhsId(houseQaStatus.getWhsId());
            asnDtl.setUdf5(data.getLgort());//移出库位 回传需要
            asnDtl.setCreateTime(ifSappullDtl.getCreateTime());
            asnDtl.setCreateUser(ifSappullDtl.getCreateUser());

            if (asnDtlRecord == null) {
                asnDtl.setCreateUser("system");
                asnDtl.setCreateTime(new Date());
                asnDtl.setQaStatus(QaStatusEnum.OK.code);
                asnDtl.setStatus( AsnDtlStatusEnum.CREATE.getCode());//新建状态
                asnDtl.setQcStatus(QcStatusEnum.已创建质检单.getCode());

                try {
                    asnDtlMapper.insert(asnDtl);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new SyncException(syncType, rsnum, e.getMessage());
                }
            } else {
                asnDtl.setModifyUser("system");
                asnDtl.setModifyTime(new Date());
                asnDtl.setAsnDtlId(asnDtlRecord.getAsnDtlId());
                try {
                    asnDtlMapper.updateById(asnDtl);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new SyncException(syncType, rsnum, e.getMessage());
                }
            }
            int row = ifSappullHeaderMapper.updateFlagById(data.getIfSappullHeaderId());
            //检查是否有同步失败的记录，有则更新为成功
            JobSyncErrorData jobSyncErrorData = jobSyncErrorDataMapper.getSyncErrorDataByTypeAndCode(syncType, rsnum);
            if (jobSyncErrorData != null && 0 == jobSyncErrorData.getIsSuccess()) {
                syncErrorDataService.updateSuccessByCode(syncType, rsnum);
            }
        }
    }
}
